package com.lxy.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxy.common.domain.common.R;
import com.lxy.common.utils.UserContext;
import com.lxy.equipment.domain.po.CompleteRepair;
import com.lxy.equipment.domain.po.Equipment;
import com.lxy.equipment.domain.po.Repair;
import com.lxy.equipment.domain.vo.CompletedRepairVO;
import com.lxy.equipment.mapper.CompleteRepairMapper;
import com.lxy.equipment.service.CompleteRepairService;
import com.lxy.equipment.service.EquipmentService;
import com.lxy.equipment.service.RepairService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class CompleteRepairServiceImpl extends ServiceImpl<CompleteRepairMapper, CompleteRepair>  implements CompleteRepairService {
    private final RepairService repairService;
    private final EquipmentService equipmentService;

    //完成订单操作
    @Override
    public void CompleteRepair(String message) {
        //repairId是设备id
        //获得参数
        String[] split = message.split("\\+");
        Long userId= Long.valueOf(split[0]);
        Long repairId=Long.valueOf(split[1]);
        Integer status=Integer.valueOf(split[2]);
        String remark=split[3];
        //创建完成订单
//        Repair repair = repairService.getById(repairId);
        LambdaQueryWrapper<Repair> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Repair::getEquipmentId,repairId);
        Repair repair = repairService.getOne(lambdaQueryWrapper);
        CompleteRepair completeRepair=new CompleteRepair();
        BeanUtils.copyProperties(repair,completeRepair);
        completeRepair.setId(null);
        completeRepair.setStatus(status);
        completeRepair.setRemark(remark);
        completeRepair.setRepairId(repairId);
        completeRepair.setCompleteTime(LocalDateTime.now());
        //保存
        save(completeRepair);
        //删除维修订单
        repairService.removeById(repair);
        //修改设备状态（如果status==1）
        if(status==1){
            Equipment equipment = equipmentService.getById(completeRepair.getEquipmentId());
            equipment.setStatus(1);
            //保存更改
            equipmentService.updateById(equipment);
        }
    }

    //获得自己的历史维修订单（维修人员）
    @Override
    public R<List<CompletedRepairVO>> getMyCompletedRepair() {
        //获取用户id
        Long userId = UserContext.getUser();
        //查找数据库
        LambdaQueryWrapper<CompleteRepair> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CompleteRepair::getUserId,userId);
        List<CompleteRepair> completeRepairs = list(lambdaQueryWrapper);
        List<CompletedRepairVO> completedRepairVOS=new ArrayList<>();
        for (CompleteRepair completeRepair : completeRepairs) {
            //进行封装
            CompletedRepairVO completedRepairVO=new CompletedRepairVO();
            BeanUtils.copyProperties(completeRepair,completedRepairVO);
            //查找设备名称
            Equipment equipment = equipmentService.getById(completeRepair.getEquipmentId());
            completedRepairVO.setEquipmentName(equipment.getEquipmentName());
            completedRepairVOS.add(completedRepairVO);
        }
        return R.ok(completedRepairVOS);
    }
}
